package com.fang.transfer.file.listener;

import com.fang.transfer.file.common.code.ClientSendCode;
import com.fang.transfer.file.common.NettyHandle;
import com.fang.transfer.file.common.RequestListener;
import com.fang.transfer.file.common.code.ServiceSendCode;
import com.fang.transfer.file.common.model.TransferFileRequest;
import com.fang.transfer.file.common.model.User;
import com.fang.transfer.file.service.FileSendRequestService;
import com.fang.transfer.file.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 发送文件同意接收
 */
@Service
public class ReceiveFileListener extends RequestListener<TransferFileRequest> {

    @Autowired
    private NettyHandle nettyHandle;

    @Autowired
    private UserService userService;

    @Autowired
    private FileSendRequestService fileSendRequestService;

    @Override
    public void handle(TransferFileRequest param) {
        TransferFileRequest request = fileSendRequestService.getById(param.getId());
        User sendUser = request.getSendUser();
        User receiveUser = request.getReceiveUser();
        request.setRole("1");
        nettyHandle.send(sendUser.getId(), ServiceSendCode.RUN_UDP_ADDR_SYN, request);
        request.setRole("2");
        nettyHandle.send(receiveUser.getId(), ServiceSendCode.RUN_UDP_ADDR_SYN, request);
    }

    @Override
    public String getCode() {
        return ClientSendCode.RECEIVE_FILE;
    }

}
